package com.public_module.basemvp.model;

import android.content.Context;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.public_module.config.ApiYJ;
import com.public_module.config.SetConfig;
import com.public_module.tools.SharedPreferencesManger;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

public abstract class BaseModel {
    protected String TAG = "BaseModel";
    protected OkHttpClient client;
    protected Gson gson;
    protected Context mContext;
    protected ApiYJ api;//异步
    protected ApiYJ apisync;//同步

    public class NullStringToEmptyAdapterFactory<T> implements TypeAdapterFactory {
        @SuppressWarnings("unchecked")
        public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
            Class<T> rawType = (Class<T>) type.getRawType();
            if (rawType != String.class) {
                return null;
            }
            return (TypeAdapter<T>) new StringNullAdapter();
        }
    }

    public class StringNullAdapter extends TypeAdapter<String> {
        @Override
        public String read(JsonReader reader) throws IOException {
            // TODO Auto-generated method stub
            if (reader.peek() == JsonToken.NULL) {
                reader.nextNull();
                return "";
            }
            return reader.nextString();
        }
        @Override
        public void write(JsonWriter writer, String value) throws IOException {
            // TODO Auto-generated method stub
            if (value == null) {
                writer.nullValue();
                return;
            }
            writer.value(value);
        }
    }

    public BaseModel(Context context)
    {
        mContext = context;
        gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
        client = new OkHttpClient.Builder()
                .connectTimeout(SetConfig.getConnectTimeout(), TimeUnit.SECONDS)
                .readTimeout(SetConfig.getReadTimeout(), TimeUnit.SECONDS)
                .writeTimeout(SetConfig.getWriteTimeout(), TimeUnit.SECONDS)
                .followRedirects(false)
                .build();

        api = new Retrofit.Builder()
                .baseUrl("http://"+ SharedPreferencesManger.getHostName()+"/")
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                //.addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build().create(ApiYJ.class);

        apisync = new Retrofit.Builder()
                .baseUrl("http://"+ SharedPreferencesManger.getHostName()+"/")
                .client(client)
                .build().create(ApiYJ.class);
    }
}
